package rollercoaster.train.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import rollercoaster.commons.Vertex3D;
import rollercoaster.train.interfaces.IMultipleSurfaceControlPointsReader;

public class OBJControlPointsMultipleSurfacesReader implements
		IMultipleSurfaceControlPointsReader {
	
	private String filepath;
	
	public OBJControlPointsMultipleSurfacesReader(){
		super();
	}

	public OBJControlPointsMultipleSurfacesReader(String filepath) {
		super();
		this.filepath = filepath;
	}
	
	public List<Vertex3D[][]> getControlPointsList(String filepath) {
		this.filepath = filepath;
		return getControlPointsList();
	}

	public List<Vertex3D[][]> getControlPointsList() {
		List<Vertex3D> vertices = new ArrayList<Vertex3D>();
		int dim_u=0;
		int dim_v=0;
		
		List<Vertex3D[][]> verticesList = new ArrayList<Vertex3D[][]>();
		
		
		
		try {
			BufferedReader reader = new BufferedReader(new FileReader(new File(filepath)));
			String s;
			try {
				while((s = reader.readLine()) != null){
					while(s!=null && !s.startsWith("v ")){
						//System.out.println(s+" ciao");
						s=reader.readLine();
					}
					vertices.clear();
					while(s!=null && s.startsWith("v ")){
						
						if(s!=null && s.startsWith("v ") && s.endsWith("\\")){
							reader.readLine();
							//System.out.println("cc");
						}
						
						
						StringTokenizer tok=new StringTokenizer(s," ");
						tok.nextToken();
						
						Vertex3D vertex=new Vertex3D(Float.parseFloat(tok.nextToken()), Float.parseFloat(tok.nextToken()), Float.parseFloat(tok.nextToken()));
						vertices.add(vertex);
						s=reader.readLine();
					}
					while(s!=null && !s.startsWith("parm u")){
						s=reader.readLine();
					}
					if(s!=null){
						
						//System.out.println("1 "+s);
						
						/*
						StringTokenizer tok2=new StringTokenizer(s," ");
						int dim_knot_u=tok2.countTokens()-2;
						if(s!=null && s.endsWith("/")){
							dim_knot_u--;
							s=reader.readLine();
							while(s!=null && s.endsWith("/")){
								StringTokenizer tokSlash=new StringTokenizer(s," ");
								dim_knot_u+=tokSlash.countTokens()-1;
							}
							dim_knot_u++;
						}
						dim_u=dim_knot_u-3;
						*/
						
						StringTokenizer tok2=new StringTokenizer(s," ");
						int dim_knot_u=tok2.countTokens()-2;
						dim_u=dim_knot_u-3;
						
						//Slash detection and removal
						if (s!=null && s.endsWith("\\")){
					    	//System.out.println(""+s);
							dim_u=dim_knot_u-3;
							dim_u--;
							
							s=reader.readLine();
							StringTokenizer tokSlash=new StringTokenizer(s," ");
							dim_u+=tokSlash.countTokens();
					    	}
						
						s=reader.readLine();
						StringTokenizer tok3=new StringTokenizer(s," ");
						int dim_knot_v=tok3.countTokens()-2;
						dim_v=dim_knot_v-3;
						
						//System.out.println("u "+dim_u+" x v "+dim_v);
						//System.out.println(vertices.size());
						
						Vertex3D[][] controlPoints = new Vertex3D[dim_v][dim_u];
						int k=0;
						int i,j;
						for (i = 0; i < dim_v; i++) {
							for (j = 0; j < dim_u; j++) {
								controlPoints[i][j]=vertices.get(k);
								k++;	
							}
						}
						
						verticesList.add(controlPoints);
					}
					
				}
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			
	
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
		return verticesList;
	}

}
